<template>
  <div>
    <TopFilter ref="topFilter" :cols="2" :list="topFilterList" @filterChange="hanldeTopfilterChange"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="repart_queryStatistics_queryEnterSituation_summary"
      :columns="columns"
      :dataSource="dataSource"
      :isExportExcel="true"
      height="auto"
      maxHeight="650"
      exportFileName="服务顾问业绩统计汇总.xlsx"
      :isMemoryPagination="true"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChangeHandle"
    >
      <template slot="controls" slot-scope="props"> </template>
    </FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 99,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import dictCode from '@/utils/dictCode';
import { arrayObjDeWeight } from '@/utils/index'; // 去重
import { getDealerUser } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import { queryServiceSummary } from '@/api/repair/queryStatistics/queryEnterSituation';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import { getQueryEnterSituation, getAllUser } from '@/api/repair/queryStatistics/queryAccounts';
export default {
  mixins: [dictionary],
  props: ['fetchParams'],
  data() {
    return {
      dataSource: [],
      columns: this.createTableCheckColumns(),
      topFilterList: this.createTopFilterList()
    };
  },
  mounted() {
    // this.getDealerUser();
    // 初始化放开服务顾问及禁用维修类型
    this.columns.find(item => item.dataIndex == 'SERVICE_ADVISOR').hidden = false;
    this.columns.find(item => item.dataIndex == 'REPAIR_TYPE_NAME').hidden = true;
    this.queryTableMainData();
    this.getAllUserData();
    console.log(this.fetchParams, '主页查询参数');
  },
  methods: {
    createTableCheckColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          width: 70,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          hidden: false,
          showOverflowTooltip: true,
          dictItems: [],
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },
        {
          title: '维修类型',
          dataIndex: 'REPAIR_TYPE_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '车次',
          dataIndex: 'TRAIN_NUMBER',
          sorter: true,
          filter: true,
          align: 'center',
          summation: true,
          showOverflowTooltip: true,
          filterType: 'number'
        },
        {
          title: '台次',
          dataIndex: 'TAIWANESE',
          sorter: true,
          filter: true,
          align: 'center',
          summation: true,
          showOverflowTooltip: true,
          filterType: 'number'
        },
        {
          title: '合计金额',
          dataIndex: 'SUM_AMOUNT',
          sorter: true,
          filter: true,
          align: 'right',
          precision: 2,
          summation: true,
          showOverflowTooltip: true,
          filterType: 'number'
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'RADIO',
          fieldName: 'groupCondition',
          itemList: [
            { text: '服务顾问', value: 'SERVICE_ADVISOR' },
            { text: '维修类型', value: 'REPAIR_TYPE_CODE' },
            { text: '服务顾问,维修类型', value: 'SERVICE_ADVISOR,REPAIR_TYPE_CODE' }
          ],
          initialValue: 'SERVICE_ADVISOR',
          labelWidth: '0px',
          change: e => {
            this.columns.find(item => item.dataIndex == 'SERVICE_ADVISOR').hidden = e.indexOf('SERVICE_ADVISOR') == -1;
            this.columns.find(item => item.dataIndex == 'REPAIR_TYPE_NAME').hidden = e.indexOf('REPAIR_TYPE_CODE') == -1;
            this.queryTableMainData();
          }
        }
      ];
    },
    // 获取所有人的信息
    /* async getDealerUser() {
      const res = await getDealerUser();
      let userData = arrayObjDeWeight(res.data, 'userId');
      this.columns.find(x => x.dataIndex == 'SERVICE_ADVISOR').dictItems = userData.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
    },*/

    // 获取全部人名
    async getAllUserData() {
      const res = await getAllUser();
      this.columns.find(item => item.dataIndex === 'SERVICE_ADVISOR').filterItems = this.columns.find(item => item.dataIndex === 'SERVICE_ADVISOR').dictItems = res.data.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
    },

    // 获取服务顾问信息

    // 查询table主数据
    async queryTableMainData() {
      let filterData = await this.$refs.topFilter.GET_FORM_DATA();
      this.$refs.table.START_LOADING();
      try {
        let res = await queryServiceSummary(Object.assign(this.fetchParams, filterData[1]));
        if (res.resultCode == 200) {
          this.dataSource = res.data;
        }
      } catch (err) {
        console.log(err, '查询table主数据失效');
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },
    // 点击顶部搜索
    hanldeTopfilterChange(e) {
      this.columns.find(item => item.dataIndex == 'SERVICE_ADVISOR').hidden = e.groupCondition.indexOf('SERVICE_ADVISOR') == -1;
      this.columns.find(item => item.dataIndex == 'REPAIR_TYPE_NAME').hidden = e.groupCondition.indexOf('REPAIR_TYPE_CODE') == -1;
      this.queryTableMainData();
    },
    selectChangeHandle(val) {
      this.selectedRow = val;
    },
    closeDialog() {
      this.$emit('close', false);
    }
  }
};
</script>
<style lang="less" scoped></style>
